package org.example.redisson;

import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.*;

/**
 * @Classname UpdateLockTimeoutTask
 * @Description 异步任务对key失效进行延期
 * @Date 2021/3/25 10:17 上午
 * @Created by xiangjd
 */
public class UpdateLockTimeoutTask implements Runnable {

    private String uuid;
    private StringRedisTemplate stringRedisTemplate;
    private String key;

    public UpdateLockTimeoutTask(String uuid, StringRedisTemplate stringRedisTemplate, String key) {
        this.uuid = uuid;
        this.stringRedisTemplate = stringRedisTemplate;
        this.key = key;
    }

    @Override
    public void run() {
        //以uuid为key,当前线程id为value放入redis
        stringRedisTemplate.opsForValue().set(uuid, String.valueOf(Thread.currentThread().getId()));
        while (true) {
            stringRedisTemplate.expire(key, 30, TimeUnit.SECONDS);
            try {
                //每隔10秒执行一次
                sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
